<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('优惠券列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: bootstrap-select-css" />

</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="shop-coupon-form">
                <div class="select-list">
                    <ul>
                        <li>
                            优惠券标题：<input type="text" name="title"/>
                        </li>
                        <li class="select-selectpicker">
                            <p style="width: 80px">适用油品： </p>
                            <select id="productIdStr" th:with="petrol=${petrolList}" class="selectpicker" data-none-selected-text="请选择" multiple>
                                <option th:each="one : ${petrol}" th:text="${one.productCategory+one.productName}" th:value="${one.id}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 80px">优惠券类型： </p>
                            <select name="couponType" th:with="type=${@dict.getType('coupon_type')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 80px">优惠券状态： </p>
                            <select name="couponStatus" th:with="type=${@dict.getType('coupon_status')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 80px">是否上架： </p>
                            <select name="isEnable" th:with="type=${@dict.getType('is_enable')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="searchPre()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="resetPre()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="back:shopCoupon:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-warning multiple disabled" onclick="selectMember()" shiro:hasPermission="back:shopCoupon:sendToMember">
                <i class="fa fa-user-circle-o"></i> 赠送优惠券
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('back:shopCoupon:edit')}]];
    var removeFlag = [[${@permission.hasPermi('back:shopCoupon:remove')}]];
    var couponStatusDict = [[${@dict.getType('coupon_status')}]];
    var couponTypeDict = [[${@dict.getType('coupon_type')}]];
    var datas = [[${@dict.getType('is_enable')}]];
    var prefix = ctx + "back/shopCoupon";

    $(function () {
        var options = {
            url: prefix + "/searchList",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "站点优惠券",
            sortName: "createTime",
            sortOrder: "desc",
            queryParams: queryParams,
            columns: [
                {
                    checkbox: true
                },
                {
                    field : 'id',
                    title : '优惠券ID',
                    visible: false
                },
                {
                    field : 'title',
                    title : '优惠券标题'
                },
                {
                    field : 'linkProduct',
                    title : '适用油品'
                },
                {
                    field : 'reachAmount',
                    title : '使用门槛',
                    formatter: function (value, row, index) {
                        if(0 == value){
                            return "无使用门槛";
                        }else{
                            return "满"+value+"元可用";

                        }
                    }
                },
                {
                    field: 'couponType',
                    title: '优惠信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<span style="font-weight: bold">' + $.table.selectDictLabel(couponTypeDict, value) + '</span>');
                        if(1 == value){
                            //满减
                            actions.push('<br/><label>面值：</label><span style="font-weight: bold">' + row.faceValue + '元</span>');
                        }else{
                            //折扣
                            actions.push('<br/><label>面值：</label><span style="font-weight: bold">' + row.discountValue + '折</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'totalIssuance',
                    title: '发放信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>总发放：</label><span style="font-weight: bold">' + value + ' 张</span>');
                        actions.push('<br/><label>已领取：</label><a href="#" onclick="showGetList(\'' + row.id + '\')" style="font-weight: bold;text-decoration:underline">' + row.totalGet + ' 张</a>');
                        actions.push('<br/><label>已使用：</label><span style="font-weight: bold">' + row.totalUse + ' 张</span>');
                        return actions.join('');
                    },
                    sortable: true
                },
                {
                    field: 'couponStatus',
                    title: '优惠券状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(couponStatusDict, value);
                    }
                },
                {
                    field: 'isEnable',
                    title: '是否上架',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas, value);
                    }
                },
                {
                    field: 'isAbleActivity',
                    title: '是否注册赠送',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas, value);
                    }
                },
                {
                    field : 'useStartTime',
                    title : '使用时间',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>开始时间：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>结束时间：</label><span style="font-weight: bold">' + row.useEndTime + '</span>');
                        return actions.join('');
                    },
                    sortable: true
                },
                {
                    field : 'createTime',
                    title : '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.productIdStr = $.common.join($('#productIdStr').selectpicker('val'));
        return search;
    }

    function searchPre() {
        $.table.search('shop-coupon-form', 'bootstrap-table');
    }

    function resetPre() {
        $("#shop-coupon-form")[0].reset();
        $("#productIdStr").selectpicker('refresh');
        $.table.search('shop-coupon-form', 'bootstrap-table');
    }

    function selectMember() {
        var rows = $("#bootstrap-table").bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一张优惠券");
            return;
        }
        var isAbleSend = true;
        $.each(rows, function(i, d) {
            if(1 != d.couponStatus){
                $.modal.alertWarning("优惠券【"+d.title+"】不是领取中的状态");
                isAbleSend=false;
                return isAbleSend;
            }
            if(0 == d.stock){
                $.modal.alertWarning("优惠券【"+d.title+"】已领完");
                isAbleSend=false;
                return isAbleSend;
            }
        });
        if(!isAbleSend){
            return;
        }
        var url = prefix + "/selectMember/" + $.table.selectFirstColumns().join();
        $.modal.open("选择受赠优惠券的会员", url, 1000);
    }

    function showGetList(couponId){
        var url = ctx + 'back/shopCoupon/getList/'+ couponId;
        var options = {
            title: "已领取该优惠券的会员列表",
            width: "1000",
            skin: 'layui-layer-gray',
            url: url,
            btn: ['关闭'],
            yes: function (index, layero) {
                $.modal.close(index);
            }
        };
        $.modal.openOptions(options);
    }
</script>
</body>
</html>